package com.lzx.adx.report.util.helper;

import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.lzs.adx.commmon.utils.JsonUtils;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.common.CommonReportRequest;
import com.lzx.adx.report.model.huawei.request.HwReportRequest;
import com.lzx.adx.report.model.ks.request.KsReportRequest;
import com.lzx.adx.report.model.qtt.QttRequest;
import com.lzx.adx.report.model.xiaomi.XiaoMiRequest;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class YtyHeper {

    public static String covertQttUrl(AccountDTO accountDTO, CommonReportRequest aqyRequest, ReportDTO reportDTO) {
        QttRequest qttRequest = JSONObject.parseObject(reportDTO.getExtend(), QttRequest.class);


        Map<String,Object> params=new HashMap<>();
        params.put("oaid",qttRequest.getOaid());
        params.put("android_id_md5",qttRequest.getHash_android_id());
        params.put("imei_md5",qttRequest.getImeiMd5());
        params.put("oaid_md5",qttRequest.getOaidMd5());
        params.put("ip",qttRequest.getIp());
        params.put("ua",qttRequest.getUser_agent());

        String callbackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());


        String urlParams= URLUtil.buildQuery(params, StandardCharsets.UTF_8)+"&callback="+URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl()+"&"+urlParams;
    }


    public static String covertKSUrl(AccountDTO accountDTO, KsReportRequest ksReportRequest, ReportDTO reportDTO) {
        String dspExtend = accountDTO.getDspExtend();
        JSONObject jsonObject = JSONObject.parseObject(dspExtend);
        Map<String, Object> params = new HashMap<>();
        params.put("adId",jsonObject.get("adId"));
        params.put("channelId",jsonObject.get("channelId"));
        params.put("type",jsonObject.get("type"));
        params.put("imei", "");
        params.put("imei_md5", StringUtils.isNotBlank(ksReportRequest.getImei()) ? ksReportRequest.getImei().toLowerCase() : "");
        params.put("oaid","");
        params.put("oaid_md5", StringUtils.isNotBlank(ksReportRequest.getOaid()) ? ksReportRequest.getOaid().toLowerCase() : "");
        params.put("idfa", "");
        params.put("idfa_md5", StringUtils.isNotBlank(ksReportRequest.getIdfa()) ? ksReportRequest.getIdfa().toLowerCase() : "");
        params.put("ip", ksReportRequest.getIp());
        params.put("ua", URLUtil.encode(ksReportRequest.getUser_agent()));
        params.put("android_id", "");
        log.info("回传给百度极速版因特云接口原始数据：{}", JsonUtils.toJsonString(params));
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "?" + urlParams;
    }

    public static String covertKSYtyUrl(AccountDTO accountDTO, KsReportRequest ksReportRequest, ReportDTO reportDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("imei", "");
        params.put("imei_md5", StringUtils.isNotBlank(ksReportRequest.getImei()) ? ksReportRequest.getImei().toLowerCase() : "");
        params.put("oaid","");
        params.put("oaid_md5", StringUtils.isNotBlank(ksReportRequest.getOaid()) ? ksReportRequest.getOaid().toLowerCase() : "");
        params.put("idfa", "");
        params.put("idfa_md5", StringUtils.isNotBlank(ksReportRequest.getIdfa()) ? ksReportRequest.getIdfa().toLowerCase() : "");
        params.put("ip", ksReportRequest.getIp());
        params.put("ua", URLUtil.encode(ksReportRequest.getUser_agent()));
        params.put("android_id", "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }



    public static String covertXiaomiUrl(AccountDTO accountDTO,ReportDTO reportDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("oaid", reportDTO.getOaid());
        params.put("android_id_md5", reportDTO.getHash_android_id());
        params.put("imei_md5", reportDTO.getImei());
        params.put("ip", reportDTO.getIp());
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }


    public static String covertHwUrl(AccountDTO accountDTO,ReportDTO reportDTO,HwReportRequest hwReportRequest) {
        Map<String, Object> params = new HashMap<>();
        params.put("oaid", reportDTO.getOaid());
        if (StringUtils.isNotBlank(reportDTO.getOaid())){
            params.put("oaid_md5", SecureUtil.md5(reportDTO.getOaid()));
        }
        if (StringUtils.isNotBlank(reportDTO.getImei()) && !reportDTO.getImei().equals("__IMEI__")) {
            params.put("imei_md5", reportDTO.getImei());
        }
        params.put("android_id_md5", StringUtils.isNotBlank(hwReportRequest.getDevice_id())?hwReportRequest.getDevice_id():"");
        params.put("ip", reportDTO.getIp());
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }


    public static String covertYouYouUrl(AccountDTO accountDTO,ReportDTO reportDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("oaid", StringUtils.isNotBlank(reportDTO.getOaid())?reportDTO.getOaid():"");
        params.put("android_id_md5", StringUtils.isNotBlank(reportDTO.getHash_android_id())?reportDTO.getHash_android_id():"");
        params.put("imei_md5", StringUtils.isNotBlank(reportDTO.getImeiSum())?reportDTO.getImeiSum():"");
        params.put("ip", StringUtils.isNotBlank(reportDTO.getIp())?reportDTO.getIp():"");
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }


}
